/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.html.chart;

import org.jxreport.core.Table;
import org.jxreport.core.views.chart.bar.BarChartView;
import org.jxreport.core.views.chart.bar.Series;
import org.jxreport.html.HTMLView;
import org.w3c.dom.Element;

public class HTMLBarChartView extends BarChartView implements HTMLView {

	public HTMLBarChartView(Element chartElement, Table table) {
		super(chartElement, table);
	}

	@Override
	public String getJQueryCode()
	{
		String code = getInputJSCode() + " " +
				"var " + super.id + " = $.jqplot('" + super.chartId + "', " + this.getInputArrayJSCode() + ", {" +
						"title:'" + super.title + "', " + 
					    "legend: {" + 
					        "show: " + this.isShowLegend() + ", " +
					        "location: 'ne', " +	// compass direction, nw, n, ne, e, se, s, sw, w.
					        "xoffset: 12, " +		// pixel offset of the legend box from the x (or x2) axis.
					        "yoffset: 12, " + 		// pixel offset of the legend box from the y (or y2) axis.
				        "}, " +
						"series:[ " + this.getSeriesJSCode() + "], " + 
				        "seriesDefaults:{ " +
				            "renderer:$.jqplot.BarRenderer, " + 
				            "rendererOptions: { ";
				if(showHorizontal) code += "barDirection: 'horizontal', "; 
				code += 	"fillToZero: true " + 
				            "} " + 
				        "}, " +
				        "axes: { " +
				        	"xaxis: { " +
								"renderer: $.jqplot.CategoryAxisRenderer, " + 
								"ticks: " + this.id  + "Ticks " +
				            "}, " + 
				            "yaxis: { " +
								"pad: 1.05, " + 
								"tickOptions: {formatString: '%d'} " +
				            "}" + 
				        "}" + 
		    	"});";

		return code;
	}

	protected String getInputJSCode() {
		String code = this.getTicksCode();
		int i=0;
		for (Series series : super.getSeries()) {
			code += "var " + super.id + "Series" + i + " = []; ";
			for (String value: series.getValues())
				code += super.id + "Series" + i + ".push(" + value + "); ";
			i++;
		}
		return code;
	}

	@Override
	public String getInnerDivCode() {
		return "<div id=\""+ super.chartId + "\"></div>";
		//return "";
	}
	
	protected String getTicksCode() {
		String code = "var " + this.id  + "Ticks = [";
		
		boolean first = true;
		for (String tick : super.getTicks()) {
			if(first) first = false;
			else code += ", ";
			
			code += "'" + tick + "'";
		}
		
		code += "]; ";
		return code;
	}
	
	protected String getInputArrayJSCode() {
		String code = "[";
		
		boolean first = true;
		for (int i = 0; i < super.getSeries().size(); i++) {
			if(first) first = false;
			else code += ", ";
			
			code += super.id + "Series" + i;			
		}
		
		code += "]";
		return code;
	}

	protected String getSeriesJSCode() {
		String code = "";
		
		boolean first = true;
		for (Series series : super.getSeries()) {
			if(first) first = false;
			else code += ", ";
			
			code += "{label:'" + series.getTitle() + "'}";			
		}
		
		return code;
	}
}
